public class O04II {
    public static void main(String[] args) {
        O04II o = new O04II();
        System.out.println(o.findNumberIn2DArray(new int[][]{
                {1, 4, 7, 11, 15},
                {2, 5, 8, 12, 19},
                {3, 6, 9, 16, 22},
                {10, 13, 14, 17, 24},
                {18, 21, 23, 26, 30}
        }, 5));
    }
        public boolean findNumberIn2DArray(int[][] matrix, int target) {
            if(matrix == null || matrix.length == 0) {
                return false;
            }
            int m = matrix.length, n = matrix[0].length;
            int row = 0, col = n - 1;
            while(row < m && col >= 0) {
                if(matrix[row][col] > target) {
                    col--;
                }else if(matrix[row][col] < target) {
                    row++;
                }else {
                    return true;
                }
            }
            return false;
        }
}
